package com.tianji.aigc.tools;

import com.tianji.aigc.constants.ToolConstants;
import com.tianji.aigc.entity.PrePlaceOrder;
import com.tianji.aigc.result.ToolResultHolder;
import com.tianji.api.client.trade.TradeClient;
import com.tianji.api.dto.trade.OrderConfirmVO;
import com.tianji.common.utils.UserContext;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class OrderTool {

    @Autowired
    private TradeClient tradeClient;


    @Tool(description = ToolConstants.Tool.PRE_PLACE_ORDER_BY_ID)
    public PrePlaceOrder preOrder(@ToolParam(description = ToolConstants.Tool_Param.COURSE_ID) Long courseId,
                                  ToolContext toolContext
    ){
        Long userId = (Long) toolContext.getContext().get("user_id");
        UserContext.setUser(userId);
        OrderConfirmVO orderConfirmVO = tradeClient.prePlaceOrder(Arrays.asList(courseId));
        PrePlaceOrder prePlaceOrder = PrePlaceOrder.of(orderConfirmVO);
        //获取大map的Id
        String requestId = (String) toolContext.getContext().get("request_id");
        //将数据封装到大map中
        ToolResultHolder.put(requestId,"prePlaceOrder",prePlaceOrder);
        return prePlaceOrder;
    }

}
